class pageHelpUtil{

  constructor(pageContent) {
    this.pageContentList = pageContent+'.list'
    this.pageContentPageNum = pageContent+'.pageNum'
    this.pageContentTotal = pageContent+'.total';
    this.pageContentLoading = pageContent+'.loading';
    this.pageContentLoaded = pageContent+'.loaded';
    this.pageContent = pageContent
  }
  load(pageHandler,that,pageContent){
    this._pageHandler = pageHandler;
    let list = that.data[this.pageContent]['list'];
    this._pageHandler().then(res => {
      let newList = list.concat(res.rows);
      if(newList.length == res.total){
        that.setData({[this.pageContentLoading]:true,[this.pageContentLoaded]:false})
      }

      that.setData({
        [this.pageContentList]:newList,
        [this.pageContentPageNum]:that.data[this.pageContent]['pageNum']+1,
        [this.pageContentTotal]:res.total
      })
    });
  }

  init(that){
    that.setData({[this.pageContentList]:[],[this.pageContentPageNum]:1,[this.pageContentTotal]:0,[this.pageContentLoading]:true,[this.pageContentLoaded]:true})
  }

  onPullDownRefresh(that){
    wx.stopPullDownRefresh() 
    that.setData({[this.pageContentList]:[],[this.pageContentPageNum]:1,[this.pageContentTotal]:0,[this.pageContentLoading]:true,[this.pageContentLoaded]:true})
    that.onLoad();
  }

  onReachBottom(that){
    if(that.data[this.pageContent]['loaded']){
      that.setData({[this.pageContentLoading]:false,[this.pageContentLoaded]:true})
      this.load(this._pageHandler,that)
    }
  }
  
}
export default  pageHelpUtil;